<?php

namespace app\admin\controller;

use app\AdminBaseController;

class Brand extends AdminBaseController{

    public function index(){
        if($this->request->isAjax()){
            $rows = $this->request->param('rows',10,'intval');
            $name = $this->request->param('name','','trim');
            $where[] = ['level','=',1];
            if($name!=''){
                $where[] = ['name','like','%'.$name.'%'];
            }
            //$status
            $res = \app\model\Brand::where($where)->order('id asc')->paginate($rows,false);
            foreach ($res as $item){
                // 2级别
                $item->children = $childs = \app\model\Brand::where(['parent_id'=>$item->id])->order('id asc')->select();
                foreach ($childs as $chi){
                    // 3系列
                    $chi->children = \app\model\Brand::where(['parent_id'=>$chi->id])->order('id asc')->select();
                }
            }

            return json(['total'=>$res->total(),'rows'=>$res->items()]);

        } else {

            return view();
        }


    }

}